/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package org.biubiu0002.baiblog.common.constants;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Random;

/**
 * 缓存、Redis所有key、lock、field常量
 *
 * @author
 */
@Component
@Data
public class CacheConstants {

    public static long SHARE_LIKE_COUNT_EXPIRE_MINUTES;
    public static long SHARE_COMMENT_COUNT_EXPIRE_MINUTES;
    public static long SHARE_FAVOR_COUNT_EXPIRE_MINUTES;
    public static long USER_INFO_EXPIRE_MINUTES;
    public static long SHARE_LIKE_COUNT_BOARD_ITEM_EXPIRE_MINUTES;
    public static long SHARE_LATEST_BOARD_ITEM_EXPIRE_MINUTES;
    public static long SHARE_INFO_EXPIRE;
    public static long SHARE_USER_FAVOR_EXPIRE_MINUTES;


    public static final String TRUE = "1";

    public static final String FALSE = "0";

    @Value("${cache.share.likeCount.expireMinutes:1440}")
    private long shareLikeCountExpireMinutes;

    @Value("${cache.share.commentCount.expireMinutes:1440}")
    private long shareCommentCountExpireMinutes;

    @Value("${cache.share.favorCount.expireMinutes:1440}")
    private long shareFavorCountExpireMinutes;

    @Value("${cache.user.userInfo.expireMinutes:10080}")
    private long userInfoExpireMinutes;

    @Value("${cache.share.boardItem.likeCount.expireMinutes:10080}")
    private long shareLikeCountBoardItemExpireMinutes;

    @Value("${cache.share.boardItem.likeCount.expireMinutes:1440}")
    private long shareLatestBoardItemExpireMinutes;

    @Value("${cache.share.info.expireMinutes:1440}")
    private long shareInfoExpireMinutes;

    @Value("${cache.share.userFavor.expireMinutes:1440}")
    private long shareUserFavorExpireMinutes;

    private static Random random = new Random();




    @PostConstruct
    public void init() {
        CacheConstants.SHARE_LIKE_COUNT_EXPIRE_MINUTES = shareLikeCountExpireMinutes;
        CacheConstants.SHARE_COMMENT_COUNT_EXPIRE_MINUTES = shareFavorCountExpireMinutes;
        CacheConstants.SHARE_FAVOR_COUNT_EXPIRE_MINUTES = shareFavorCountExpireMinutes;
        CacheConstants.USER_INFO_EXPIRE_MINUTES = userInfoExpireMinutes;
        CacheConstants.SHARE_LIKE_COUNT_BOARD_ITEM_EXPIRE_MINUTES = shareLikeCountBoardItemExpireMinutes;
        CacheConstants.SHARE_LATEST_BOARD_ITEM_EXPIRE_MINUTES = shareLatestBoardItemExpireMinutes;
        CacheConstants.SHARE_INFO_EXPIRE = shareInfoExpireMinutes;
        CacheConstants.SHARE_USER_FAVOR_EXPIRE_MINUTES = shareUserFavorExpireMinutes;
    }

    public static final String SEPARATOR = "::";

    /**
     * 登录token
     */
    public static String getTokenKey(String token) {
        return String.format("BAIBLOG:TOKEN:%s", token);
    }

    /**
     * 用户信息key
     */
    public static String getUserInfoKey(String userUid) {
        return String.format("BAIBLOG:USER:INFO:%s", userUid);
    }

    /**
     * 用户关注列表key
     */
    public static String getUserFollowListKey(String uid) {
        return String.format("BAIBLOG:USER:FOLLOWLIST:%s", uid);
    }

    /**
     * 用户粉丝列表key
     */
    public static String getUserFansListKey(String uid) {
        return String.format("BAIBLOG:USER:FANSLIST:%s", uid);
    }

    /**
     * 推文信息key
     */
    public static String getShareInfoKey(String shareId) {
        return String.format("BAIBLOG:SHARE:INFO:%s", shareId);
    }

    /**
     * 推文点赞数key
     */
    public static String getShareLikeCountKey(String shareId) {
        return String.format("BAIBLOG:SHARE:LIKECOUNT:%s", shareId);
    }

    /**
     * 推文评论数key
     */
    public static String getShareCommentCountKey(String shareId) {
        return String.format("BAIBLOG:SHARE:COMMENTCOUNT:%s", shareId);
    }

    /**
     * 推文收藏数key
     */
    public static String getShareFavorCountKey(String shareId) {
        return String.format("BAIBLOG:SHARE:FAVORCOUNT:%s", shareId);
    }

    /**
     * 点赞map
     * key: userid::shareId
     * value: 1:点赞 0 取消点赞
     */
    public static String getShareLikeMapKey() {
        return "BAIBLOG:SHARE:LIKEMAP";
    }


    /**
     * 收藏map
     * key: userid::shareId
     * value: 1:收藏 0 取消收藏
     */
    public static String getShareFavorMapKey() {
        return "BAIBLOG:SHARE:FAVORMAP";
    }

    /**
     * 用户的收藏set
     * value: 1:收藏 0 取消收藏
     */
    public static String getShareUserFavorKey(String userUid) {
        return String.format("BAIBLOG:SHARE:USERFAVOR:%s", userUid);
    }

    /**
     * user::share 的field
     */
    public static String getUserShareField(String userId, String shareId) {
        return String.format("%s::%s", userId, shareId);
    }

    /**
     * 榜单itemKey
     */
    public static String getShareBoardItemKey(String shareId) {
        return String.format("BAIBLOG:SHARE:BOARD:ITEM:%s", shareId);
    }

    /**
     * 每周推文点赞榜key
     */
    public static String getWeeklyShareLikeCountBoardKey() {
        return "BAIBLOG:SHARE:BOARD:LIKECOUNT";
    }

    /**
     * 最新发布榜key
     */
    public static String getShareLatestBoardKey() {
        return "BAIBLOG:SHARE:BOARD:LATEST";
    }

    /**
     * feed流收信箱key
     */
    public static String getUserFeedRXKey(String uid) {
        return String.format("BAIBLOG:USER:FEEDRX:%s",uid);
    }

    /**
     * 分享基本信息分布式锁
     */
    public static String getShareInfoLock(String shareId) {
        return String.format("BAIBLOG:SHARE:INFO:LOCK:%s", shareId);
    }

    /**
     * 推文点赞数分布式锁
     */
    public static String getShareLikeCountLock(String shareId) {
        return String.format("BAIBLOG:SHARE:LOCK:LIKECOUNT:%s", shareId);
    }

    /**
     * 推文评论数分布式锁
     */
    public static String getShareCommentCountLock(String shareId) {
        return String.format("BAIBLOG:SHARE:LOCK:COMMENTCOUNT:%s", shareId);
    }

    /**
     * 推文收藏数分布式锁
     */
    public static String getShareFavorCountLock(String shareId) {
        return String.format("BAIBLOG:SHARE:LOCK:FAVORCOUNT:%s", shareId);
    }

    /**
     * 用户信息缓存过期时间
     */
    public static long getUserInfoExpireMinutes() {
        return USER_INFO_EXPIRE_MINUTES + random.nextInt((int) USER_INFO_EXPIRE_MINUTES);
    }

    /**
     * 推文缓存过期时间
     */
    public static long getShareInfoExpireMinutes() {
        return SHARE_INFO_EXPIRE + random.nextInt((int) SHARE_INFO_EXPIRE);
    }

    /**
     * 每周点赞榜item缓存过期时间
     */
    public static long getShareLikeCountBoardItemExpireMinutes() {
        return SHARE_LIKE_COUNT_BOARD_ITEM_EXPIRE_MINUTES + random.nextInt((int) SHARE_LIKE_COUNT_BOARD_ITEM_EXPIRE_MINUTES);
    }

    /**
     * 每日最新发布榜item缓存过期时间
     */
    public static long getShareLatestBoardItemExpireMinutes() {
        return SHARE_LATEST_BOARD_ITEM_EXPIRE_MINUTES + random.nextInt((int) SHARE_LATEST_BOARD_ITEM_EXPIRE_MINUTES);
    }

    /**
     * 推文点赞数缓存过期时间
     */
    public static long getShareLikeCountExpireMinutes() {
        return SHARE_LIKE_COUNT_EXPIRE_MINUTES + random.nextInt((int) SHARE_LIKE_COUNT_EXPIRE_MINUTES);
    }

    /**
     * 推文评论数缓存过期时间
     */
    public static long getShareCommentCountExpireMinutes() {
        return SHARE_COMMENT_COUNT_EXPIRE_MINUTES + random.nextInt((int) SHARE_COMMENT_COUNT_EXPIRE_MINUTES);
    }

    /**
     * 推文收藏数缓存过期时间
     */
    public static long getShareFavorCountExpireMinutes() {
        return SHARE_FAVOR_COUNT_EXPIRE_MINUTES + random.nextInt((int) SHARE_FAVOR_COUNT_EXPIRE_MINUTES);
    }

    /**
     * 用户推文收藏关系set过期时间
     */
    public static long getShareUserFavorExpireMinutes() {
        return SHARE_USER_FAVOR_EXPIRE_MINUTES + random.nextInt((int) SHARE_USER_FAVOR_EXPIRE_MINUTES);
    }


    /**
     * 离线消息存储key
     * @param uid 接受用户id
     * @return key
     */
    public static String getOffRxKey(String uid){
        return String.format("BAIBLOG:IM:OFFRX:%s",uid);
    }

    /**
     * 用户最后一条消息key
     * map<sessionId,latestMsg>
     * @param uid uid
     * @return key
     */
    public static String getLatestConversationKey(String uid){
        return String.format("BAIBLOG:IM:LATESTCONVERSATION:%s",uid);
    }

    /**
     * 用户未读数量key
     * map<sessionId,int>
     * @param uid
     * @return
     */
    public static String getUnreadKey(String uid) {
        return String.format("BAIBLOG:IM:UNREAD:%s",uid);
    }


}
